home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.03 Mar 89 / Basic⁄Pascal Source Code / ADB Pascal Sample / myADBStuff < prev   
Encoding:
Text File  |  1989-02-07  |  13.6 KB  |  525 lines  |  [TEXT/PJMM]

  1. unit MyADBStuff;
  2.  
  3. interface
  4.  
  5.     uses
  6.         OSIntf, PrintTraps, MyADBGlobals;
  7.  
  8.     procedure doMessage (message0: str255; message1: str255; message2: str255; message3: str255);
  9.     procedure doAbout;
  10.     procedure doQuit;
  11.     procedure doMenubar (menuResult: LongInt);
  12.     procedure doContent (ConEvent: EventRecord; contentWindow: windowPtr);
  13.     procedure doDrag (GrabWindow: WindowPtr; GlobalMouse: point);
  14.     procedure doGrow (ResizeWindow: WindowPtr; Globalmouse: point; Zoomflg: Boolean);
  15.  
  16. implementation
  17.  
  18.     procedure doMessage; {(message0 : str255}
  19.                                     {message1 : str255}
  20.                                     {message2 : str255}
  21.                                     {message3 : str255)}
  22.         var
  23.             dialogP: DialogPtr;
  24.             item: integer;
  25.     begin
  26.         ParamText(message0, message1, message2, message3);
  27.         dialogP := GetNewDialog(MessageDialog, nil, pointer(-1));
  28.         if dialogP = nil then
  29.             begin
  30.                 SysBeep(5);
  31.                 ExitToShell;
  32.             end;
  33.         initCursor; {change to arrow}
  34.         ModalDialog(nil, item);
  35.         DisposDialog(dialogP);
  36.     end;
  37.  
  38.     procedure delay;
  39.         const
  40.             stalltime = 15; {quarter seconds in ticks}
  41.         var
  42.             i: LongInt;
  43.             tick1, tick2: LongInt;
  44.     begin
  45.         tick1 := TickCount;
  46.         tick2 := tick1;
  47.         repeat
  48.             tick2 := TickCount;
  49.         until tick2 >= tick1 + stalltime;
  50.     end;
  51.  
  52.     procedure togglelightsoff;
  53.         const
  54.             flushcommandNum = $21;  { 00100001 }
  55.     begin
  56.         Buf.opServiceRtPtr := Info.dbServiceRtPtr;    {service routine pointer}
  57.         Buf.opDataAreaPtr := Info.dbDataAreaAddr;   {optional data area address}
  58.         OSEr := ADBOp(nil, nil, Buf.dataBuffPtr, flushcommandNum);  { Flush command, clears the device }
  59.         if OSEr = -1 then
  60.             doMessage('Unable to flush device', '', '', '');
  61.     end;
  62.  
  63.     procedure togglelightson;
  64.         var
  65.             LEDaddress: ptr;
  66.             LEDbyte: signedbyte;
  67.         const
  68.             talkcommandNum = $2E;  { 00101010 }
  69.             listencommandNum = $2A; { 00101110 }
  70.     begin
  71.         if OSEr = noErr then
  72.             begin
  73.                 Buf.opServiceRtPtr := Info.dbServiceRtPtr;    {service routine pointer}
  74.                 Buf.opDataAreaPtr := Info.dbDataAreaAddr;   {optional data area address}
  75.                 togglelightsoff;
  76.                 OSEr := ADBOp(Buf.opDataAreaPtr, Buf.opServiceRtPtr, Buf.dataBuffPtr, talkcommandNum);
  77.                 if OSEr = -1 then
  78.                     begin
  79.                         doMessage('Unable to talk to device', '', '', '');
  80.                         exit(togglelightson);
  81.                     end;
  82.                 delay;
  83.                 LEDaddress := POINTER(ORD(Buf.dataBuffPtr) + 2);
  84.                 LEDbyte := LEDaddress^;
  85.                 if LEDbyte = -1 then
  86.                     LEDaddress^ := -8; {turn on}
  87.                 OSEr := ADBOp(Buf.opDataAreaPtr, Buf.opServiceRtPtr, Buf.dataBuffPtr, listencommandNum);
  88.                 if OSEr = -1 then
  89.                     begin
  90.                         doMessage('Unable to listen to device', '', '', '');
  91.                         exit(togglelightson);
  92.                     end;
  93.             end; {of noErr}
  94.     end;
  95.  
  96.     procedure LightsMagic;
  97.     begin
  98.         togglelightson;
  99.         delay;
  100.         togglelightsoff;
  101.         delay;
  102.         togglelightson;
  103.         delay;
  104.         togglelightsoff;
  105.         delay;
  106.         togglelightson;
  107.         delay;
  108.         togglelightsoff;
  109.         delay;
  110.         togglelightson;
  111.         delay;
  112.         togglelightsoff;
  113.         delay;
  114.     end;
  115.  
  116.     procedure QuerySystem;
  117.         var
  118.             str1, str2: str255;
  119.     begin
  120.         NumToString(LongInt(theWorld.environsVersion), str2);
  121.         str1 := concat('Environment Version = ', str2, chr(13));
  122.         TEInsert(pointer(ord4(@str1) + 1), Length(str1), MyTextHandle);
  123.  
  124.         case theWorld.machineType of
  125.             0: 
  126.                 str2 := 'new version of Macintosh';
  127.             1: 
  128.                 str2 := 'Macintosh 512K enhanced';
  129.             2: 
  130.                 str2 := 'Macintosh Plus';
  131.             3: 
  132.                 str2 := 'Macintosh SE';
  133.             4: 
  134.                 str2 := 'Macintosh II';
  135.             otherwise
  136.                 begin
  137.                     if theWorld.machineType = -1 then
  138.                         str2 := 'Macintosh with 64K ROM';
  139.                     if theWorld.machineType = -2 then
  140.                         str2 := 'Macintosh XL';
  141.                 end;
  142.         end; {of case}
  143.         str1 := concat('Machine Type = ', str2, chr(13));
  144.         TEInsert(pointer(ord4(@str1) + 1), Length(str1), MyTextHandle);
  145.  
  146.         NumToString(LongInt(theWorld.systemVersion), str2);
  147.         str1 := concat('System Version (must convert to hex) = ', str2, chr(13));
  148.         TEInsert(pointer(ord4(@str1) + 1), Length(str1), MyTextHandle);
  149.  
  150.         case theWorld.processor of
  151.             0: 
  152.                 str2 := 'new processor';
  153.             1: 
  154.                 str2 := 'MC68000 processor';
  155.             2: 
  156.                 str2 := 'MC68010 processor';
  157.             3: 
  158.                 str2 := 'MC68020 processor';
  159.             4: 
  160.                 str2 := 'MC68030 processor';
  161.             otherwise
  162.                 begin
  163.                     str2 := 'unknown processor';
  164.                 end;
  165.         end; {of case}
  166.         str1 := concat('Processor = ', str2, chr(13));
  167.         TEInsert(pointer(ord4(@str1) + 1), Length(str1), MyTextHandle);
  168.  
  169.         NumToString(LongInt(theWorld.hasFPU), str2);
  170.         str1 := concat('Has Floating Point Coprocessor (1=Y) = ', str2, chr(13));
  171.         TEInsert(pointer(ord4(@str1) + 1), Length(str1), MyTextHandle);
  172.  
  173.         NumToString(LongInt(theWorld.hasColorQD), str2);
  174.         str1 := concat('Has Color QuickDraw (1=Y) = ', str2, chr(13));
  175.         TEInsert(pointer(ord4(@str1) + 1), Length(str1), MyTextHandle);
  176.  
  177.         case theWorld.KeyBoardType of
  178.             0: 
  179.                 str2 := 'Macintosh Plus keyboard with keypad';
  180.             1: 
  181.                 str2 := 'Macintosh keyboard';
  182.             2: 
  183.                 str2 := 'Macintosh keyboard and keypad';
  184.             3: 
  185.                 str2 := 'Macintosh Plus keyboard';
  186.             4: 
  187.                 str2 := 'Apple extended keyboard';
  188.             5: 
  189.                 str2 := 'Standard Apple Desktop Bus keyboard';
  190.             otherwise
  191.                 begin
  192.                     str2 := 'Unknown keyboard value';
  193.                 end;
  194.         end; {of case}
  195.         str1 := concat('Keyboard Type = ', str2, chr(13));
  196.         TEInsert(pointer(ord4(@str1) + 1), Length(str1), MyTextHandle);
  197.  
  198.         NumToString(LongInt(theWorld.atDrvrVersNum), str2);
  199.         str1 := concat('Appletalk Driver Version = ', str2, chr(13));
  200.         TEInsert(pointer(ord4(@str1) + 1), Length(str1), MyTextHandle);
  201.  
  202.         NumToString(LongInt(theWorld.sysVRefNum), str2);
  203.         str1 := concat('Working Directory Volume Ref = ', str2, chr(13));
  204.         TEInsert(pointer(ord4(@str1) + 1), Length(str1), MyTextHandle);
  205.     end;
  206.  
  207.     procedure doADB;
  208.         var
  209.             devTableIndex: integer;
  210.             str1: str255;
  211.     begin
  212.         TESetSelect(0, TEMax, MyTextHandle);
  213.         TEDelete(MyTextHandle);
  214.         ShowWindow(ADBWindow);
  215.  
  216.         OSEr := SysEnvirons(versRequested, theWorld);
  217.         if OSEr = envNotPresent then
  218.             doMessage('System File older than 4.1!', '', '', '');
  219.         if OSEr = envBadVers then
  220.             doMessage('Bad Version Requested', '', '', '');
  221.         if OSEr = envVersTooBig then
  222.             doMessage('Requested Version Not Available', '', '', '');
  223.         if OSEr = noErr then
  224.             begin {Query and Blinking routine}
  225.  
  226.                 QuerySystem;
  227.  
  228.         {toggle keyboard lights on and off}
  229.                 if theWorld.keyBoardType = 4 then
  230.                     begin {keyboard type 4}
  231.                         str1 := concat(chr(13), 'Watch the keyboard lights blink…  ', chr(13));
  232.                         TEInsert(pointer(ord4(@str1) + 1), Length(str1), MyTextHandle);
  233.  
  234.                         numberofADBs := CountADBs;
  235.                         for devTableIndex := 1 to numberofADBs do
  236.                             begin {devTableIndex}
  237.                                 ADBAddr := GetIndADB(info, devTableIndex); { find the device address }
  238.                                 if ADBAddr = 2 then
  239.                                     begin {ADBAddr}
  240.                                         device := info.devType;
  241.                                         LightsMagic;
  242.                                     end; {of ADBAddr}
  243.                             end; {devTableIndex}
  244.                     end; {keybaord type 4}
  245.             end; { of noErr}
  246.     end; {of Proc}
  247.  
  248.     procedure doAbout;
  249.         var
  250.             IDStrHandle: StringHandle;
  251.             dialogP: DialogPtr;
  252.             item: integer;
  253.             Str1, Str2, Str3: str255;
  254.             myHeapSpace: LongInt;
  255.             FreeSpace: Size;
  256.     begin
  257.         IDStrHandle := StringHandle(GetResource(rsrc, 0));
  258.         if IDStrHandle = nil then
  259.             begin
  260.                 doMessage('Get About box crash!', '', '', '');
  261.                 ExitToShell;
  262.             end;
  263.         MoveHHi(Handle(IDStrHandle));
  264.         HLock(Handle(IDStrHandle));
  265.         FreeSpace := FreeMem;
  266.         myHeapSpace := MaxMem(FreeSpace);
  267.         NumToString(myHeapSpace, Str2);
  268.         Str2 := concat('Memory = ', Str2);
  269.         Str3 := '';
  270.         Str1 := '';
  271.         ParamText(IDStrHandle^^, Str1, Str2, Str3);
  272.         dialogP := GetNewDialog(AboutDialog, nil, pointer(-1));
  273.         if dialogP = nil then
  274.             begin
  275.                 doMessage('Dialog crash!', 'We are dead...', '', '');
  276.                 ExitToShell;
  277.             end;
  278.         initCursor;
  279.         ModalDialog(nil, item);
  280.         DisposDialog(dialogP);
  281.         HUnlock(Handle(IDStrHandle));
  282.     end;
  283.  
  284.     procedure doQuit;
  285.     begin
  286.         DisposeWindow(ADBWindow);
  287.         TEDispose(MyTextHandle);
  288.         Finished := true;
  289.     end; {of proc}
  290.  
  291.     procedure doSave;
  292.     begin
  293.     end;
  294.  
  295.     procedure doSaveAs;
  296.     begin
  297.     end;
  298.  
  299.     procedure doPrint;
  300.     begin
  301.     end;
  302.  
  303.     procedure doPageSet;
  304.     begin
  305.     end;
  306.  
  307.     procedure doMenubar; {(menuResult : LongInt)}
  308.         var
  309.             theMenu: integer;
  310.             theItem: integer;
  311.             daName: STR255;
  312.             accItem: integer;
  313.             temp: GrafPtr;
  314.             dummy: LongInt;    {Desk Scrap result var}
  315.             ScrapReturn: OSErr; {TEScrap result var}
  316.             TextLength: integer;
  317.             ScrapLength: LongInt;
  318.     begin
  319.         theMenu := HiWord(menuResult); {menu}
  320.         theItem := LoWord(menuResult); {item}
  321.         case theMenu of
  322.             AppleMenu: 
  323.                 begin
  324.                     if theItem = aAbout then
  325.                         doAbout
  326.                     else
  327.                         begin    {must be DA}
  328.                             GetItem(myMenus[AppleM], theItem, daName);
  329.                             GetPort(temp);    {protect against flacky DA}
  330.                             accItem := OpenDeskAcc(daName);
  331.                             SetPort(temp);
  332.                         end; {else}
  333.                 end; {of AppleMenu}
  334.             FileMenu: 
  335.                 begin
  336.                     case theItem of
  337.                         fADB: 
  338.                             begin
  339.                                 doADB;
  340.                             end;
  341.                         fSave: 
  342.                             begin
  343.                                 doSave;
  344.                             end;
  345.                         fSaveAs: 
  346.                             begin
  347.                                 doSaveAs;
  348.                             end;
  349.                         fPageSet: 
  350.                             begin
  351.                                 doPageSet;
  352.                             end;
  353.                         fPrint: 
  354.                             begin
  355.                                 doPrint;
  356.                             end;
  357.                         fQuit: 
  358.                             begin
  359.                                 doQuit;
  360.                             end;
  361.                         otherwise
  362.                             begin
  363.                             end;
  364.                     end; {of theitem}
  365.                 end; {of FileMenu}
  366.             EditMenu: 
  367.                 begin
  368.                     if not SystemEdit(theitem - 1) then
  369.                         begin
  370.                             case theItem of
  371.                                 eUndo: 
  372.                                     begin
  373.                                         doMessage('Undo not available.', '', '', '');
  374.                                     end;
  375.                                 eCut: 
  376.                                     begin
  377.                                         TECut(MyTextHandle);
  378.                                         dummy := ZeroScrap;
  379.                                         ScrapReturn := TEToScrap; {update desk scrap}
  380.                                     end;
  381.                                 eCopy: 
  382.                                     begin
  383.                                         TECopy(MyTextHandle);
  384.                                         dummy := ZeroScrap;
  385.                                         ScrapReturn := TEToScrap; {update desk scrap}
  386.                                     end;
  387.                                 ePaste: 
  388.                                     begin
  389.                                         ScrapReturn := TEFromScrap;
  390.                                         TextLength := MyTextHandle^^.teLength;
  391.                                         ScrapLength := TEGetScrapLen;
  392.                                         if (LongInt(TextLength + ScrapLength)) > longInt(TEMax - 1) then
  393.                                             begin
  394.                                                 initCursor;
  395.                                                 paramText('Paste would exceed text edit 32000 buffer limit!', '', '', '');
  396.                                                 ItemHit := StopAlert(AlertDialog, nil);
  397.                                             end
  398.                                         else
  399.                                             TEPaste(MyTextHandle);
  400.                                     end; { of paste}
  401.                                 eClear: 
  402.                                     begin
  403.                                         TEDelete(MyTextHandle);
  404.                                     end;
  405.                                 otherwise
  406.                                     begin
  407.                                     end;
  408.                             end; {of case}
  409.                         end; {of system edit}
  410.                 end; {of EditMenu}
  411.             otherwise
  412.                 begin
  413.                 end;
  414.         end; {of theMenu}
  415.         HiliteMenu(0);    {un-hilite selected menu}
  416.     end;
  417.  
  418.     procedure doContent; {(ConEvent : EventRecord}
  419.                                     {contentWindow : windowPtr);}
  420.         var
  421.             localPt, globalPt: Point;
  422.             part: integer;
  423.             myRect: Rect;
  424.             control: ControlHandle;
  425.     begin
  426.         if contentWindow <> FrontWindow then
  427.             SelectWindow(contentWindow);
  428.         globalPt := ConEvent.where;
  429.         localPt := globalPt;        {global coord of mouse}
  430.         GlobalToLocal(localPt);     {local coord of mouse}
  431.         part := FindControl(localPt, contentWindow, control);
  432.  
  433.         if contentWindow = ADBWindow then
  434.             begin
  435.                 SetPort(ADBWindow);
  436.                 if part <> 0 then
  437.                     begin {in control}
  438.                     end;
  439.                 if part = 0 then
  440.                     begin    {content region}
  441.                         myRect := ADBWindow^.portRect;
  442.                         if PtInRect(localPt, myRect) then
  443.                             begin
  444.                                 TEClick(localPt, BitAnd(ConEvent.modifiers, ShiftKey) = ShiftKey, myTextHandle)
  445.                             end;  {of ptInRect}
  446.                     end; { of part=0 }
  447.             end; {of contentwindow}
  448.     end;  {of proc}
  449.  
  450.     procedure doDrag; {(GrabWindow : WindowPtr}
  451.                                 {GlobalMouse : point);}
  452.     begin
  453.         DragWindow(GrabWindow, GlobalMouse, DragArea);
  454.     end;
  455.  
  456.     procedure doGrow; {(ResizeWindow : WindowPtr;}
  457.                                 {Globalmouse : point;}
  458.                                 {ZoomFlg:Boolean);}
  459.         var
  460.             newSize: LongInt;
  461.             hsize: integer;
  462.             vsize: integer;
  463.             oldPort: GrafPtr;
  464.             myRect: rect;
  465.             tempLong: LongInt;
  466.             l, t, r, b: LongInt;
  467.     begin
  468.         if (ResizeWindow <> FrontWindow) then
  469.             SelectWindow(ResizeWindow)
  470.         else
  471.             begin
  472.                 if (ZoomFlg) then
  473.                     begin
  474.                         with ResizeWindow^.portRect do
  475.                             begin
  476.                                 tempLong := bottom - top;
  477.                                 newSize := BitShift(tempLong, 16);
  478.                                 newSize := newSize + (right - left);
  479.                             end;
  480.                     end
  481.                 else
  482.                     newSize := GrowWindow(ResizeWindow, Globalmouse, GrowArea);
  483.                 if newSize <> 0 then
  484.                     begin  {grow the window}
  485.                         hsize := LoWord(newSize);
  486.                         vsize := HiWord(newSize);
  487.                         if ResizeWindow = ADBWindow then
  488.                             begin
  489.                                 with ResizeWindow^.portRect do {Pre-Grow}
  490.                                     begin
  491.                                         SetRect(VCRect, right - (SBarWidth - 1), top - 1, right + 1, bottom - (SBarWidth - 2));
  492.                                         SetRect(HCRect, left - 1, bottom - (SBarWidth - 1), right - (SBarWidth - 2), bottom + 1);
  493.                                         SetRect(GrowRect, HCRect.right, HCRect.top, VCRect.right, HCRect.bottom);
  494.                                     end;  {of with }
  495.                                 SizeWindow(ResizeWindow, hsize, vsize, TRUE); {new portRect}
  496.                                 InvalRect(GrowRect);
  497.                                 EraseRect(GrowRect);
  498.                                 with ResizeWindow^.portRect do {Post Grow}
  499.                                     begin
  500.                                         SetRect(VCRect, right - (SBarWidth - 1), top - 1, right + 1, bottom - (SBarWidth - 2));
  501.                                         SetRect(HCRect, left - 1, bottom - (SBarWidth - 1), right - (SBarWidth - 2), bottom + 1);
  502.                                         SetRect(GrowRect, HCRect.right, HCRect.top, VCRect.right, HCRect.bottom);
  503.                                         SetRect(ViewRect, left + 4, top + 4, right - (SBarWidth - 1), bottom - (SBarWidth - 1));
  504.                                     end;  {of with }
  505.  
  506.                                 InvalRect(GrowRect);  {needed for update on shrink}
  507.                                 HideControl(VControl);
  508.                                 HideControl(HControl);
  509.                                 MoveControl(VControl, VCRect.left, VCRect.top);
  510.                                 MoveControl(HControl, HCRect.left, HCRect.top);
  511.                                 SizeControl(VControl, SBarWidth, VCRect.bottom - VCRect.top);
  512.                                 SizeControl(HControl, HCRect.right - HCRect.left, SBarWidth);
  513.                                 ShowControl(VControl);
  514.                                 ShowControl(HControl);
  515.                                 ValidRect(VCRect);
  516.                                 ValidRect(HCRect);
  517.  
  518.                                 MyTextHandle^^.ViewRect := ViewRect;
  519.                                 InValRect(ViewRect);
  520.                             end; {of if ResizeWindow}
  521.                     end; {of grow window stuff}
  522.             end; {of if then newsize}
  523.     end;  { of proc }
  524.  
  525. end. {of unit}